﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Collections.Generic;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using CaseStudy.Web.UserControl;

using ISS.CaseStudy.BLL;
using ISS.CaseStudy.Entities;

namespace CaseStudy.Web
{
    public partial class CategoryView : System.Web.UI.Page
    {
        private string cnSiteRoot = System.Configuration.ConfigurationManager.AppSettings["CNSiteRoot"].ToString();
        private string caseContentFolder = System.Configuration.ConfigurationManager.AppSettings["CaseContentFolder"].ToString();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["type"] == null)
                return;

            string category = Request.QueryString["type"];

            if (category.ToLower().Equals("industry"))
            {
                List<CaseStudyInfo> list = new List<CaseStudyInfo>();
                DataSet dataSet = CaseStudyBLL.GetAllOrderBy(CaseStudyOrder.Industry);
                if (dataSet.Tables.Count == 0) return;
                DataTable table = dataSet.Tables[0];

                int count = CaseStudyBLL.GetCountOf("Industry");
                int leftCount = count / 2;
                int rightCount = count - leftCount;
                int flag = 0;
                bool newIndustry = false;

                string currentType = string.Empty;
                string currentClient = string.Empty;
                Panel panel = null;

                foreach (DataRow row in table.Rows)
                {
                    if ((row["IndustryName"] != null) &&
                        (!string.IsNullOrEmpty(row["IndustryName"].ToString())))
                    {
                        string industry = row["IndustryName"].ToString();

                        if (!currentType.Equals(industry))
                        {
                            flag++;
                            newIndustry = true;

                            if (!string.IsNullOrEmpty(currentType) &&
                                panel != null)
                            {
                                if (flag <= leftCount + 1)
                                {
                                    divCategoryLeft.Controls.Add(panel);
                                }
                                else
                                {
                                    divCategoryRight.Controls.Add(panel);
                                }
                            }

                            currentType = industry;

                            panel = new Panel();
                            panel.ID = industry;
                            panel.CssClass = "CategoryItemContent";

                            Label label = new Label();
                            label.Text = industry;
                            label.CssClass = "CategoryItemTitle";
                            panel.Controls.Add(label);
                        }
                        else
                        {
                            newIndustry = false;
                        }

                        string client = row["ClientName"].ToString();

                        if (!currentClient.Equals(client) || newIndustry)
                        {
                            currentClient = client;

                            Label label = new Label();
                            label.Text = client;
                            label.CssClass = "ItemSecondLevelName";

                            panel.Controls.Add(label);
                        }

                        HyperLink linkTitle = new HyperLink();
                        linkTitle.Text = row["Title"].ToString();
                        linkTitle.ToolTip = row["Title"].ToString();
                        linkTitle.NavigateUrl = cnSiteRoot + "/CaseStudyView.aspx?id=" + row["Id"].ToString();
                        linkTitle.CssClass = "ItemTitle";
                        panel.Controls.Add(linkTitle);
                    }

                    if ((table.Rows.IndexOf(row) + 1 == table.Rows.Count) &&
                        (panel != null))
                    {
                        if (flag <= leftCount)
                        {
                            divCategoryLeft.Controls.Add(panel);
                        }
                        else
                        {
                            divCategoryRight.Controls.Add(panel);
                        }
                    }
                }
            }
            else if (category.ToLower().Equals("client"))
            {
                List<CaseStudyInfo> list = new List<CaseStudyInfo>();
                DataSet dataSet = CaseStudyBLL.GetAllOrderBy(CaseStudyOrder.Client);
                if (dataSet.Tables.Count == 0) return;
                DataTable table = dataSet.Tables[0];

                int count = CaseStudyBLL.GetCountOf("Client");
                int rightCount = count / 2;
                int leftCount = count - rightCount;
                int flag = 0;
                bool newClient = false;

                string currentType = string.Empty;
                string currentIndustry = string.Empty;
                Panel panel = null;

                foreach (DataRow row in table.Rows)
                {
                    if ((row["ClientName"] != null) &&
                        (!string.IsNullOrEmpty(row["ClientName"].ToString())))
                    {
                        string client = row["ClientName"].ToString();

                        if (!currentType.Equals(client))
                        {
                            flag++;
                            newClient = true;

                            if (!string.IsNullOrEmpty(currentType) &&
                                panel != null)
                            {
                                if (flag <= leftCount + 1)
                                {
                                    divCategoryLeft.Controls.Add(panel);
                                }
                                else
                                {
                                    divCategoryRight.Controls.Add(panel);
                                }
                            }

                            currentType = client;

                            panel = new Panel();
                            panel.ID = client;
                            panel.CssClass = "CategoryItemContent";

                            Label label = new Label();
                            label.Text = client;
                            label.CssClass = "CategoryItemTitle";
                            panel.Controls.Add(label);
                        }
                        else
                        {
                            newClient = false;
                        }

                        string industry = row["IndustryName"].ToString();

                        if (!currentIndustry.Equals(industry) || newClient)
                        {
                            currentIndustry = industry;

                            Label label = new Label();
                            label.Text = industry;
                            label.CssClass = "ItemSecondLevelName";

                            panel.Controls.Add(label);
                        }

                        HyperLink linkTitle = new HyperLink();
                        linkTitle.Text = row["Title"].ToString();
                        linkTitle.ToolTip = row["Title"].ToString();
                        linkTitle.NavigateUrl = cnSiteRoot + "/CaseStudyView.aspx?id=" + row["Id"].ToString();
                        linkTitle.CssClass = "ItemTitle";
                        panel.Controls.Add(linkTitle);
                    }

                    if ((table.Rows.IndexOf(row) + 1 == table.Rows.Count) &&
                        (panel != null))
                    {
                        if (flag <= leftCount)
                        {
                            divCategoryLeft.Controls.Add(panel);
                        }
                        else
                        {
                            divCategoryRight.Controls.Add(panel);
                        }
                    }
                }
            }
            else if (category.ToLower().Equals("service"))
            {
                List<CaseStudyInfo> list = new List<CaseStudyInfo>();
                DataSet dataSet = CaseStudyBLL.GetAllOrderBy(CaseStudyOrder.Service);
                if (dataSet.Tables.Count == 0) return;
                DataTable table = dataSet.Tables[0];

                int count = CaseStudyBLL.GetCountOf("Service");
                int leftCount = count / 2 - 1;
                int rightCount = count - leftCount + 1;
                int flag = 0;
                bool newService = false;

                string currentType = string.Empty;
                string currentClient = string.Empty;
                Panel panel = null;

                foreach (DataRow row in table.Rows)
                {
                    if ((row["ServiceName"] != null) &&
                        (!string.IsNullOrEmpty(row["ServiceName"].ToString())))
                    {
                        string service = row["ServiceName"].ToString();

                        if (!currentType.Equals(service))
                        {
                            flag++;
                            newService = true;

                            if (!string.IsNullOrEmpty(currentType) &&
                                panel != null)
                            {
                                if (flag <= leftCount + 1)
                                {
                                    divCategoryLeft.Controls.Add(panel);
                                }
                                else
                                {
                                    divCategoryRight.Controls.Add(panel);
                                }
                            }

                            currentType = service;

                            panel = new Panel();
                            panel.ID = service;
                            panel.CssClass = "CategoryItemContent";

                            Label label = new Label();
                            label.Text = service;
                            label.CssClass = "CategoryItemTitle";
                            panel.Controls.Add(label);
                        }
                        else
                        {
                            newService = false;
                        }

                        string client = row["ClientName"].ToString();

                        if (!currentClient.Equals(client) || newService)
                        {
                            currentClient = client;

                            Label label = new Label();
                            label.Text = client;
                            label.CssClass = "ItemSecondLevelName";

                            panel.Controls.Add(label);
                        }

                        HyperLink linkTitle = new HyperLink();
                        linkTitle.Text = row["Title"].ToString();
                        linkTitle.ToolTip = row["Title"].ToString();
                        linkTitle.NavigateUrl = cnSiteRoot + "/CaseStudyView.aspx?id=" + row["Id"].ToString();
                        linkTitle.CssClass = "ItemTitle";
                        panel.Controls.Add(linkTitle);
                    }

                    if ((table.Rows.IndexOf(row) + 1 == table.Rows.Count) &&
                        (panel != null))
                    {
                        if (flag <= leftCount)
                        {
                            divCategoryLeft.Controls.Add(panel);
                        }
                        else
                        {
                            divCategoryRight.Controls.Add(panel);
                        }
                    }
                }
            }
        }
    }
}